<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 11:02
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;


use app\common\business\Admin as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\captcha\facade\Captcha;

class Login extends Base
{
    protected $busObj = null;

    public function __construct(App $app)
    {
        $this->busObj = new BusObj();
        parent::__construct($app);
    }

    public function index()
    {
        if(!empty($this->request->admin)){
            return redirect((string)url('index/index'));
        }
        return $this->fetch();
    }

    public function captcha()
    {
        return Captcha::create("admin_login");
    }

    public function checkLogin()
    {
        $move_user = $this->request->param('move_user', '', 'trim');
        $move_pwd = $this->request->param('move_pwd', '', 'trim');
        $captcha = $this->request->param('captcha', '', 'trim');
        $arg = $this->request->param('arg',0,'intval');
        $data = [
            "move_user" => $move_user,
            "move_pwd" => $move_pwd,
            "captcha" => $captcha,
            "arg"=>$arg
        ];

        try {
            $result = $this->busObj->checkLogin($data);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($result) {
            return Show::json_show(Status::status_normal, "登录成功");
        } else {
            return Show::json_show(Status::status_error, "登录失败！请重试");
        }
    }
}